//
//  HPCustomerSnapshotCell.m
//  HappyPenguin
//
//  Created by zhuangyihang on 1/10/16.
//  Copyright © 2016 zhuangyihang. All rights reserved.
//

#import "HPCustomerSnapshotCell.h"

#import "UIConstants.h"
#import "NSString+Common.h"

@interface HPCustomerSnapshotLabel : UIView

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *detailLabel;

@end

static const NSInteger kCellSeperatorHeight = 10;
static const NSInteger kCellMarginLeft = 15;
static const NSInteger kTitleLabelMargin = 15;
static const NSInteger kSeperatorViewMargin = 10;

static const NSInteger kTitleFontSize = 17;
static const NSInteger kItemLabelHeight = 20;

@interface HPCustomerSnapshotCell()

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) UILabel *titleLabel;
//@property (nonatomic, strong) NSMutableArray *starViews;

@property (nonatomic, strong) NSMutableArray *itemLabels;

@end

@implementation HPCustomerSnapshotCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
        
        self.mainView = [UIView new];
        [self.contentView addSubview:self.mainView];
        [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentView).offset(-kCellMarginLeft*2);
            make.height.equalTo(self.contentView).offset(-kCellSeperatorHeight);
            make.bottom.centerX.equalTo(self.contentView);
        }];
        self.mainView.backgroundColor = [UIColor whiteColor];
        self.mainView.layer.borderWidth = 1;
        self.mainView.layer.borderColor = [UIColor colorWithRGB:0xefefef].CGColor;
        
        UIImageView *icon = [UIImageView new];
        [self.mainView addSubview:icon];
        [icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mainView).offset(kTitleLabelMargin);
            make.top.equalTo(self.mainView).offset(kTitleLabelMargin);
//            make.size.mas_equalTo(CGSizeMake(20,20));
        }];
        icon.image = [UIImage imageNamed:@"Customer_SnapshotIcon"];
        
        self.titleLabel = [UILabel new];
        [self.mainView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(icon.mas_right).offset(2);
            make.centerY.equalTo(icon);
        }];
        self.titleLabel.font = [UIConstants getFont:kTitleFontSize];
        self.titleLabel.textColor = [UIConstants getAppColor];
        self.titleLabel.text = @"标题";
        
        UIView *sep = [UIView new];
        [self.mainView addSubview:sep];
        [sep mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.mainView).offset(-2*kTitleLabelMargin);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(kSeperatorViewMargin);
            make.centerX.equalTo(self.mainView);
            make.height.equalTo(@1);
        }];
        sep.backgroundColor = [UIConstants getSeperatorColor];
        
//        self.starViews = [NSMutableArray array];
        UIView *last = nil;
//        for (int i = 0; i < 5; i++) {
//            UIImageView *v=  [UIImageView new];
//            [self.mainView addSubview:v];
//            [v mas_makeConstraints:^(MASConstraintMaker *make) {
//                make.bottom.equalTo(self.titleLabel);
//                make.right.equalTo(last==nil?sep:last.mas_left);
//            }];
//            
//            [self.starViews addObject:v];
//            
//            v.image = [UIImage imageNamed:@"Cell_Star"];
//            
//            last = v;
//        }
        
        last = nil;
        self.itemLabels = [NSMutableArray array];
        for (int i = 0; i < 3; i++) {
            HPCustomerSnapshotLabel *l = [HPCustomerSnapshotLabel new];
            [self.mainView addSubview:l];
            [self.itemLabels addObject:l];
            [l mas_makeConstraints:^(MASConstraintMaker *make) {
                if (last==nil) {
                    make.top.equalTo(sep.mas_bottom).offset(kSeperatorViewMargin);
                }else{
                    make.top.equalTo(last.mas_bottom);
                }
                
                make.height.mas_equalTo(kItemLabelHeight);
                make.centerX.equalTo(self.mainView);
                make.width.equalTo(sep);
            }];
            
            l.titleLabel.text = @"111";
            l.detailLabel.text = @"222";
            
            last = l;
        }
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (CGFloat)getCellHeight{
    NSString *s = @"标题";
    CGFloat height = [s getHeightWithWidth:100 withFont:[UIConstants getFont:kTitleFontSize]];

    return kCellSeperatorHeight+kTitleLabelMargin*4+ height + kItemLabelHeight*3 + 1;
}

@end


@implementation HPCustomerSnapshotLabel

- (id)init{
    self = [super init];
    if (self) {
        self.titleLabel = [UILabel new];
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.centerY.equalTo(self);
        }];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.font = [UIConstants getFont:13];
        
        self.detailLabel = [UILabel new];
        [self addSubview:self.detailLabel];
        [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self);
            make.centerY.equalTo(self);
        }];
        self.detailLabel.textColor = [UIColor blackColor];
        self.detailLabel.font = [UIConstants getFont:13];
    }
    return self;
}

@end
